package UILayer;
import CtrLayer.*;
import ModLayer.*;
import java.util.Scanner;

/**
 * Write a description of class EmployeeUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class EmployeeUI
{
    private PersonCtr personCtr;
    private ProductUI productUi;
    private LeaseUI leaseUi;
    private SaleUI saleUi;

    public EmployeeUI()
    {
        personCtr = personCtr.getInstance();
        productUi = new ProductUI();
        saleUi = new SaleUI();
        leaseUi = new LeaseUI();
    }

    public void start()
    {
        boolean finished = false;
        while(!finished)
        {
            printMenu();
            switch(reader(5))
            {
                case "0": finished = true; break;
                case "1": saleUi.start(); break;
                case "2": customerManagement(); break;
                case "3": productUi.start(); break;
                case "4": leaseUi.start(); break;
                case "5": showDiscount(); break;
            }
        }
    }

    private void printMenu()
    {
        System.out.println("\f*** Employee menu ***");
        System.out.println("\nWhat would you like to do?");
        System.out.println("(0) Back to main menu.");
        System.out.println("(1) Sale management.");
        System.out.println("(2) Customer management");
        System.out.println("(3) Inventory management.");
        System.out.println("(4) Lease management.");
        System.out.println("(5) Show fixed discount.");
        System.out.println("Make your choice: ");
    }

    private String reader2()
    {
        Scanner command = new Scanner(System.in);
        System.out.print(">> ");
        String c = command.nextLine();
        return c;
    }

    private String reader(int limitIndex)
    {
        final String[] validNumbers = {"0","1","2","3","4","5","6","7","8","9"};
        Scanner command = new Scanner(System.in);
        String c = null;
        boolean finished = false;
        while(!finished)
        {
            System.out.print("\n> ");
            c = command.nextLine();
            for(int i=0; i<=limitIndex; i++)
            {
                if(validNumbers[i].equals(c))
                    finished = true;
            }
            if(finished == false)
            {
                System.out.print("\nTry once again");
                c = null;
            }
        }
        return c;
    }

    private void pause()
    {
        boolean finished = false; 
        while(!finished)
        {
            System.out.println("\nPress something to back.");
            if(reader2().startsWith(""))
                finished = true;
        }
    }

    private void showAllCustomersForCustomerManagement()
    {
        System.out.println("\f*** Show all customers ***");
        int numbering = 1;
        for(PersonInformations personObj: personCtr.getPeopleFromSpecificClass("Customer"))
        {
            Customer cObj = (Customer) personObj;
            System.out.println("\nCustomer "+numbering+":");
            System.out.println("Name of cutomer: "+personObj.getName());
            System.out.println("Date of registration: "+cObj.getDateOfRegistration());
            System.out.println("Number of cutomer: "+cObj.getCustomerNumber());
            numbering++;
        }
    }

    private void getInfoCustomerForCustomerManagement(String customerNo)
    {
        System.out.println("Name of cutomer: "+personCtr.getInfoCustomer(customerNo, "2"));
        System.out.println("Address of cutomer: "+personCtr.getInfoCustomer(customerNo, "0"));
        System.out.println("Phone number of cutomer: "+personCtr.getInfoCustomer(customerNo, "3"));
        System.out.println("Email of cutomer: "+personCtr.getInfoCustomer(customerNo, "1"));
        System.out.println("Date of registration: "+personCtr.getInfoCustomer(customerNo, "5"));
        System.out.println("Owe of the customer: "+personCtr.getInfoCustomer(customerNo, "4"));
    }

    private void customerManagement()
    {
        boolean finished = false;
        System.out.println("\f");
        while(!finished)
        {
            System.out.println("\n*** Customer management ***"); 
            System.out.println("\nWhat would you like to do?");
            System.out.println("(0) Back to employee menu.");
            System.out.println("(1) Show all customers.");
            System.out.println("(2) Add customer.");
            System.out.println("(3) Delete customer.");
            System.out.println("(4) Update customer.");
            System.out.println("(5) Show all informations about customer.");
            System.out.println("Make your choice: ");
            switch(reader(5)) 
            { 
                case "0": finished = true; break;
                case "1": showAllCustomersForCustomerManagement(); break;

                case "2":
                System.out.println("\f*** Add customer ***");
                System.out.println("Type in name of customer");
                String name = reader2();
                System.out.println("\nType in email of customer");
                String email = reader2();
                System.out.println("\nType in address of customer");
                String address = reader2();
                System.out.println("\nType in phone number of customer");
                String phoneNo = reader2();
                personCtr.createCustomer(name, email, address, phoneNo);
                System.out.println("\fCustomer was created.");
                break;

                case "3":
                System.out.println("\f*** Delete customer ***");
                System.out.println("Type in customer number: ");
                String deleteCustomer = reader2();
                if(personCtr.searchPerson(deleteCustomer, "employee")!=null)
                {
                    personCtr.deletePerson(deleteCustomer, "employee");
                    System.out.println("\fCustomer was deleted.");
                }
                else
                    System.out.println("\fCustomer not found, try once again.");
                break;

                case "4":
                System.out.println("\f*** Update customer ***");
                System.out.println("Type in customer number: ");
                String customerNo = reader2();
                boolean finished2 = false;
                if(personCtr.searchPerson(customerNo, "employee")!=null)
                {
                    while(!finished2)
                    {
                        System.out.println("\f*** Update customer "+customerNo+" ***");
                        getInfoCustomerForCustomerManagement(customerNo);

                        System.out.println("\nWhat would you like to update?");
                        System.out.println("(0) Back to customer management.");
                        System.out.println("(1) Address.");
                        System.out.println("(2) Email.");
                        System.out.println("(3) Name.");
                        System.out.println("(4) Phone number.");
                        System.out.println("Make your choice: ");
                        switch(reader(5)) 
                        {
                            case "0": finished2 = true; break; 
                            case "1":
                            System.out.println("\fType in new address of customer:");
                            String addresss = reader2();
                            personCtr.updateCustomer(customerNo, addresss, "0");
                            break;

                            case "2":
                            System.out.println("\fType in new email of customer:");
                            String emaill = reader2();
                            personCtr.updateCustomer(customerNo, emaill, "1");
                            break;

                            case "3":
                            System.out.println("\fType in new name of customer:");
                            String namee = reader2();
                            personCtr.updateCustomer(customerNo, namee, "2");
                            break;

                            case "4":
                            System.out.println("\fType in new phone number of customer:");
                            String phoneNumberr = reader2();
                            personCtr.updateCustomer(customerNo, phoneNumberr, "3");
                            break;

                            case "5": showAllCustomersForCustomerManagement(); break;
                        }
                    }
                }
                else
                    System.out.println("\fCustomer not found, try once again.");
                break;

                case "5":
                System.out.println("\fType in customer number: ");
                String customerNoo = reader2();
                if(personCtr.searchPerson(customerNoo, "employee")!=null)
                {
                    System.out.println("\f*** Show all informations ***");
                    System.out.println("Customer number: "+customerNoo);
                    getInfoCustomerForCustomerManagement(customerNoo);
                }
                else
                    System.out.println("\fCustomer not found, try once again.");
                break;
            }
        }
    }

    private void showDiscount()
    {
        System.out.println("\f*** Show fixed discount ***"); 
        System.out.println("\nValue of discount - "+personCtr.getInfoCompany("6")+"%");
        pause();
    }
}